/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentacion;

import Negocio.Empresa;
import Negocio.Parcela;
import Negocio.Plantacion;
import java.util.Iterator;
import javax.swing.DefaultListModel;

/**
 *
 * @author Diego
 */
public class FindParcela extends javax.swing.JDialog {

    /**
     * Creates new form FindParcela
     */
    
    private Empresa unaEmpresa;
    private Plantacion unaPlantacion;
    private Parcela unaParcela;
    private FrmAplicacion parent;
    private DefaultListModel modelParcelas = new DefaultListModel();
    private DefaultListModel modelPlantaciones = new DefaultListModel();
    
    public FindParcela(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    public FindParcela(FrmAplicacion parent, boolean modal,Empresa unaEmpresa) {
        super(parent, modal);
        initComponents();
        this.parent = parent;
        this.unaEmpresa = unaEmpresa;
        this.LstParcelas.setModel(modelParcelas);
        this.LstPlantaciones.setModel(modelPlantaciones);
        this.cargarPlantaciones();
        
               
    }
    public void cargarParcelas(){
        this.modelParcelas.clear();
        if (this.unaPlantacion.getParcelas() != null && this.unaPlantacion.getParcelas().size()> 0){
            Iterator it = this.unaPlantacion.getParcelas().iterator();
            while(it.hasNext()){
                this.modelParcelas.addElement((Parcela) it.next());
            }
        }else{
            this.modelParcelas.addElement("No se Encontraron Parcelas");
        }
    }
    
    public void cargarPlantaciones(){
        this.modelPlantaciones.clear();
        if(this.unaEmpresa.getPlantacionesActivas()!= null && this.unaEmpresa.getPlantacionesActivas().size() > 0){
            Iterator it = this.unaEmpresa.getPlantacionesActivas().iterator();
            while(it.hasNext()){
                this.modelPlantaciones.addElement((Plantacion) it.next());
            }
        }else{
            this.modelPlantaciones.addElement("No se encontraron plantaciones");
        }
    }
    
    public void enviarParcela(){
        if(this.unaParcela != null){
            this.parent.setParcela(this.unaParcela);
            this.setVisible(false);
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        LstParcelas = new javax.swing.JList();
        BtnAceptar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        LstPlantaciones = new javax.swing.JList();
        Plantaciones = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        LstParcelas.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        LstParcelas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LstParcelasMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(LstParcelas);

        BtnAceptar.setText("Aceptar");
        BtnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnAceptarActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Parcelas");

        LstPlantaciones.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        LstPlantaciones.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LstPlantacionesMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(LstPlantaciones);

        Plantaciones.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        Plantaciones.setText("Plantaciones");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(BtnAceptar)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(Plantaciones))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(Plantaciones))
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane2)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BtnAceptar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void LstPlantacionesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_LstPlantacionesMouseClicked
        // TODO add your handling code here:
        this.unaPlantacion = (Plantacion) this.modelPlantaciones.getElementAt(this.LstPlantaciones.getSelectedIndex());
        this.cargarParcelas();
    }//GEN-LAST:event_LstPlantacionesMouseClicked

    private void LstParcelasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_LstParcelasMouseClicked
        // TODO add your handling code here:
        if(this.modelParcelas.size() > 0){
            this.unaParcela = (Parcela) this.modelParcelas.getElementAt(this.LstParcelas.getSelectedIndex());
            this.enviarParcela();
        }
    }//GEN-LAST:event_LstParcelasMouseClicked

    private void BtnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnAceptarActionPerformed
        // TODO add your handling code here:
        if(this.unaParcela != null){
            this.enviarParcela();
        }
    }//GEN-LAST:event_BtnAceptarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void mains(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FindParcela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FindParcela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FindParcela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FindParcela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                FindParcela dialog = new FindParcela(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        }); */
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnAceptar;
    private javax.swing.JList LstParcelas;
    private javax.swing.JList LstPlantaciones;
    private javax.swing.JLabel Plantaciones;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables
}
